package com.prism.moodme.datastore;

import java.util.Random;

public class GeoGenerator {
	
	private static double PI=3.14159265358979323846;
	static boolean deviateAvailable=false;	//	flag
	static float storedDeviate;			//	deviate from previous calculation
	private static double mu=0f;
	private static double sigma=1f;
	
	public static double randn_trig(double _mu, double _sigma) {
		mu=_mu; sigma=_sigma;
		double dist, angle;
		
		//	If no deviate has been stored, the standard Box-Muller transformation is 
		//	performed, producing two independent normally-distributed random
		//	deviates.  One is stored for the next round, and one is returned.
		if (!deviateAvailable) {
			
			//	choose a pair of uniformly distributed deviates, one for the
			//	distance and one for the angle, and perform transformations
			dist=Math.sqrt( -2.0 * Math.log(Math.random()));
			angle=2.0 * PI * (Math.random());
			
			//	calculate and store first deviate and set flag
			storedDeviate=(float) (dist*Math.cos(angle));
			deviateAvailable=true;
			
			//	calculate return second deviate
			return dist * Math.sin(angle) * sigma + mu;
		}
		
		//	If a deviate is available from a previous call to this function, it is
		//	returned, and the flag is set to false.
		else {
			deviateAvailable=false;
			return storedDeviate*sigma + mu;
		}
	}
	
	public static double randn_trig() {
		double dist, angle;
		
		//	If no deviate has been stored, the standard Box-Muller transformation is 
		//	performed, producing two independent normally-distributed random
		//	deviates.  One is stored for the next round, and one is returned.
		if (!deviateAvailable) {
			
			//	choose a pair of uniformly distributed deviates, one for the
			//	distance and one for the angle, and perform transformations
			dist=Math.sqrt( -2.0 * Math.log(Math.random()));
			angle=2.0 * PI * (Math.random());
			
			//	calculate and store first deviate and set flag
			storedDeviate=(float) (dist*Math.cos(angle));
			deviateAvailable=true;
			
			//	calcaulate return second deviate
			return dist * Math.sin(angle) * sigma + mu;
		}
		
		//	If a deviate is available from a previous call to this function, it is
		//	returned, and the flag is set to false.
		else {
			deviateAvailable=false;
			return storedDeviate*sigma + mu;
		}
	}

}
